package com.wxg.gui.table;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import com.wxg.domain.table.TableDomain;

public class JTableExample2 {

	private JFrame jframe;
	
	private TableDomain tabledatas;
	
	private JTable jtable;
	
	private AbstractTableModel tableModel;
	
	private JButton btnAdd;
	
	private JButton btnRemove;
	
	private JDialogExample jde;
	
	public JTableExample2(){
		tabledatas = new TableDomain();
		jframe = new JFrame();
	}
	
	private void addButtons(){
		/**
		 * 无参构造方法默认是采用的FlowLayout。
		 */
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
		
		btnAdd = new JButton("Add");
		btnRemove = new JButton("Remove");
		btnRemove.setEnabled(false);
		
		
		northPanel.add(btnAdd);
		northPanel.add(btnRemove);
		jframe.add(northPanel, BorderLayout.NORTH);
	}
	
	private void addJTable(){
		tableModel = new MyTableModel();
		jtable = new JTable(tableModel);
		jframe.add(new JScrollPane(jtable), BorderLayout.CENTER);
	}
	
	private void initDialog(){
		if(null == jde){
			jde = new JDialogExample(jframe, tabledatas, tableModel);
			jde.init();
		}
		jde.show();
	}
	
	private void bindEvent4btnAdd(){
		/**
		 * 
		 */
		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
//				tabledatas.data("wxg", 32, "你好！");
//				tableModel.fireTableDataChanged();
				initDialog();
			}
		});
		
		/**
		 * 
		 */
		btnRemove.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ListSelectionModel lsm = jtable.getSelectionModel();
				List<Integer> removeIndexs = new ArrayList<Integer>();
				int min = lsm.getMinSelectionIndex();
				int max = lsm.getMaxSelectionIndex();
				//System.out.printf("min: %d, max: %d\n", min, max);
				for(int i=min; i<=max; i++){
					if(lsm.isSelectedIndex(i)){
						removeIndexs.add(i);
					}
				}
				if(removeIndexs.size() == 1){
					tabledatas.remove(removeIndexs.get(0));
				}else{
					tabledatas.removes(removeIndexs.toArray(new Integer[]{}));
				}
				tableModel.fireTableDataChanged();
			}
		});
	}
	
	private void bindEvent4JTable(){
		jtable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				ListSelectionModel lsm = (ListSelectionModel)e.getSource();
				//System.out.printf("lsm.isSelectionEmpty() : %b\n", lsm.isSelectionEmpty());
				if(lsm.isSelectionEmpty()){
					btnRemove.setEnabled(false);
				}else{
					btnRemove.setEnabled(true);
				}
			}
		});
	}
	
	public void init(){
		String title = "Hello JTable";
		jframe.setTitle(title);
		
		addButtons();
		addJTable();
		
		bindEvent4btnAdd();
		bindEvent4JTable();
		
		jframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		jframe.setSize(500, 300);
		jframe.setVisible(true);
	}
	
	class MyTableModel extends AbstractTableModel{

		private static final long serialVersionUID = 1185810000761339461L;

		public MyTableModel(){
			tabledatas.head("Name", "Age", "Message");
		}
		
		
		@Override
		public String getColumnName(int column) {
			return tabledatas.head()[column];
		}

		@Override
		public int getRowCount() {
			return tabledatas.data().length;
		}

		@Override
		public int getColumnCount() {
			return tabledatas.head().length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return tabledatas.data()[rowIndex][columnIndex];
		}
	}

	public JFrame getJframe() {
		return jframe;
	}
	
	
	
}
